package com.zmn.plat.services.impl.channel.mark;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.channel.mark.ChannelMarkGroup;
import com.zmn.plat.model.entity.channel.mark.ChannelMarkGroupQuery;
import com.zmn.plat.persistence.interfaces.channel.mark.ChannelMarkGroupDao;
import com.zmn.plat.services.interfaces.channel.ChannelService;
import com.zmn.plat.services.interfaces.channel.mark.ChannelMarkGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import com.zmn.common.constant.GlobalConsts;

/**
 * 描述: 渠道标示管理渠道接口实现类
 *
 * @author jiafei
 * @since 2021/12/06 11:23
 */
@Slf4j
@Service
public class ChannelMarkGroupServiceImpl implements ChannelMarkGroupService {

    private static final String TAG = "渠道标示管理渠道基础服务接口" ;

    @Resource
    private ChannelMarkGroupDao channelMarkGroupDao;

    @Resource
    private ChannelService channelService;

    @Override
    public ChannelMarkGroup findById(Integer relateId) {

        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id查询数据relateId为null", TAG);
            return null;
        }
        return channelMarkGroupDao.findById(relateId);
    }

    @Override
    public List<ChannelMarkGroup> listByIds(List<Integer> relateIds) {

        if (CollectionUtils.isEmpty(relateIds)) {
            log.error("[{}],按条件查询数据,relateIds为null", TAG);
            return Collections.emptyList();
        }
        return channelMarkGroupDao.listByIds(relateIds);
    }

    @Override
    public List<ChannelMarkGroup> listByQuery(ChannelMarkGroupQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        return channelMarkGroupDao.listByQuery(query);
    }

    @Override
    public List<ChannelMarkGroup> listPageByQuery(ChannelMarkGroupQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        query.setTotalCount(channelMarkGroupDao.countByQuery(query));
        return channelMarkGroupDao.listPageByQuery(query);
    }

    @Override
    public Integer countByQuery(ChannelMarkGroupQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return channelMarkGroupDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(ChannelMarkGroup channelMarkGroup) {

        if (Objects.isNull(channelMarkGroup.getRelateId())){
            log.error("[{}],根据id修改数据,channelMarkGroup为null", TAG);
            return null;
        }
        return channelMarkGroupDao.updateByKey(channelMarkGroup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyAssChannels(List<Integer> assIds, Integer markId, List<Integer> cancelIds, LoginStaff operator) {

        // 取消
        if (CollectionUtil.isNotNullOrEmpty(cancelIds)) {
            channelMarkGroupDao.deleteBatchByChannelIdAndMarkId(cancelIds, markId);
        }

        if (CollectionUtil.isNotNullOrEmpty(assIds)) {
            // 去重
            List<Integer> dupChannelIds = channelMarkGroupDao.listByQuery(ChannelMarkGroupQuery.builder().channelIds(assIds).markId(markId).build()).stream().map(ChannelMarkGroup::getChannelId).collect(Collectors.toList());
            if (CollectionUtil.isNotNullOrEmpty(dupChannelIds)) {
                assIds.removeAll(dupChannelIds);
            }
            if (CollectionUtil.isNullOrEmpty(assIds)) {
                return;
            }
            Map<Integer, String> channelMap = channelService.listByChannelIds(assIds).stream()
                    .collect(Collectors.toMap(Channel::getChannelId, Channel::getName, (x, y) -> y));
            List<ChannelMarkGroup> assChannelMarkGroup = assIds.stream().map(x ->
                    ChannelMarkGroup.builder().channelId(x).channelName(channelMap.get(x)).markId(markId)
                            .creater(operator.getDeptName() + "-" + operator.getRealName()).createTime(DateUtil.getNow())
                            .updater(operator.getDeptName() + "-" + operator.getRealName()).updateTime(DateUtil.getNow())
                            .build()).collect(Collectors.toList());
            channelMarkGroupDao.insertBatch(assChannelMarkGroup);
        }

    }

    @Override
    public Integer insert(ChannelMarkGroup channelMarkGroup) {

        if (Objects.isNull(channelMarkGroup)) {
            log.error("[{}],新增数据,channelMarkGroup为null", TAG);
            return null;
        }
        return channelMarkGroupDao.insert(channelMarkGroup);
    }

    @Override
    public void insertBatch(List<ChannelMarkGroup> list) {

        if (CollectionUtils.isEmpty(list)) {
            log.error("[{}],数据为空", TAG);
            return;
        }
        channelMarkGroupDao.insertBatch(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateChannelAssMark(Integer oldMarkId, Integer newMarkId, List<Integer> channelList, LoginStaff operator) {

        List<ChannelMarkGroup> channelMarkGroups = channelMarkGroupDao.listByQuery(ChannelMarkGroupQuery.builder().channelIds(channelList).markId(oldMarkId).build());
        // 删除旧关联
        channelMarkGroupDao.deleteBatchByChannelIdAndMarkId(channelList, oldMarkId);
        // 建立新关联
        Date now = DateUtil.getNow();
        channelMarkGroups.forEach(x -> {
            x.setRelateId(null);
            x.setMarkId(newMarkId);
            x.setCreater(operator.getDeptName() + "-" + operator.getRealName());
            x.setCreateTime(now);
            x.setUpdater(operator.getDeptName() + "-" + operator.getRealName());
            x.setUpdateTime(now);
        });
        channelMarkGroupDao.insertBatch(channelMarkGroups);
        return Boolean.TRUE;
    }

    @Override
    public Integer deleteByKey(Integer relateId) {

        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id删除数据,relateId为null", TAG);
            return null;
        }
        return channelMarkGroupDao.deleteByKey(relateId);
    }

}